package org.zjx.action.route;

import org.zjx.action.Action;
import org.zjx.action.basic.DelayAction;
import org.zjx.action.basic.ScreenshotAction;
import org.zjx.action.game.ClickAddressAction;
import org.zjx.action.game.DialogueAction;
import org.zjx.action.game.GameAction;
import org.zjx.analysis.GameAnalysis;
import org.zjx.analysis.window.NpcDialogueAnalysis;
import org.zjx.core.IWindow;
import org.zjx.entity.Address;

import java.util.List;

public class NpcTransferAction extends Action {
    private final Address targetAddress;
    private final Address standPosition;
    private final Address clickPosition;
    private final List<String> dialogues;

    private NpcDialogueAnalysis npcDialogueAnalysis;

    private void analysis() {
        npcDialogueAnalysis = new NpcDialogueAnalysis(targetWindow, stateManager.getGameState().getScreenshot());
    }

    public NpcTransferAction(IWindow targetWindow, Address targetAddress, Address standPosition, Address clickPosition, List<String> dialogues) {
        super(targetWindow, false);
        this.targetAddress = targetAddress;
        this.standPosition = standPosition;
        this.clickPosition = clickPosition;
        this.dialogues = dialogues;
    }



    @Override
    protected void doExecute() throws Exception {
        while (!getAddressNow().near(targetAddress,3)){
            if (!getAddressNow().near(standPosition,3)){
                RunningAction runAction = new RunningAction(targetWindow,standPosition);
                runAction.execute();
                new DelayAction(targetWindow,3000);
            }else {
                analysis();
                if (!npcDialogueAnalysis.isOpened()){
                    ClickAddressAction clickAction = new ClickAddressAction(targetWindow,clickPosition);
                    clickAction.execute();
                }else {
                    DialogueAction dialogueAction = new DialogueAction(targetWindow,dialogues);
                    dialogueAction.execute();
                }
                new DelayAction(targetWindow,500);
            }
        }
    }

    public Address getAddressNow() {
        new ScreenshotAction(targetWindow).execute();
        new GameAction(targetWindow).execute();
        return targetWindow.getState().getGameState().getAddress();
    }
}
